package com.example.phpclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.HashMap;


public class register_activity extends AppCompatActivity {
    RequestQueue queue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        queue = Volley.newRequestQueue(getApplicationContext());
        Button s_button = (Button)findViewById(R.id.s_button);
        final EditText s_no = (EditText)findViewById(R.id.s_no);
        final EditText s_name = (EditText)findViewById(R.id.s_name);
        final EditText s_class = (EditText)findViewById(R.id.s_class);
        final EditText s_password = (EditText)findViewById(R.id.s_password);

        Spinner sex_spin = (Spinner)findViewById(R.id.sex_spin);
        Spinner level_spin = (Spinner)findViewById(R.id.level_spin);

        final String[] sexItems = getResources().getStringArray(R.array.sex);
        final String[] levelItems = getResources().getStringArray(R.array.level);

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,sexItems);
        adapter1.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);//下拉菜单样式
        sex_spin.setAdapter(adapter1);//用起来

        ArrayAdapter<String>adapter2 = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,levelItems);
        adapter2.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
        level_spin.setAdapter(adapter2);


        final Log_user user = new Log_user();
        final Student student = new Student();

        sex_spin.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//试试两种listener
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                student.setS_sex(sexItems[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        level_spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                user.setLevel(levelItems[i]);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        s_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                student.setS_no(s_no.getText().toString());
                student.setS_name(s_name.getText().toString());
                student.setS_class(s_class.getText().toString());
                user.setPassword(s_password.getText().toString());
                user.setUserNo(s_no.getText().toString());
                jsonRequest("register",student,user);
            }
        });

    }

    public void jsonRequest(String aim,Student student, Log_user user){//aim用于选择模式注册还是登录
        String url= this.getString(R.string.recUrl);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("forwhat",aim);
//        params.put("userNo",user.getUserNo());
//        params.put("password",user.getPassword());
//        params.put("level",user.getLevel());
        String userString = JSON.toJSONString(user);
        String studentString = JSON.toJSONString(student);
        params.put("student",studentString);
        params.put("user",userString);
        JSONObject jsonObject = new JSONObject(params);

        Log.d("Tag",jsonObject.toString());
        final JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log_user user1 = JSON.parseObject(jsonObject.toString(), Log_user.class);//第一个参数是String的json
                        Log.d("TAG登录人",user1.getLoged());
                        if (user1.getLoged().equals("yes")){//java判断字符串相等不能用“==”
                            //存储到pref
                            SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                            editor.putString("userNo",user1.getUserNo());
                            editor.putString("password",user1.getPassword());
                            editor.putString("level",user1.getLevel());
                            editor.apply();
                            Intent intent = new Intent(register_activity.this,Login_activity.class);//跳转到登录
                            startActivity(intent);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError");
                    }
                });
        queue.add(jsObjRequest);
    }
}
